package com.wlt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.pojo.SeckillGoods;
import com.wlt.result.BaseResult;
import com.wlt.service.SeckillGoodsService;
import com.wlt.service.SeckillService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 秒杀商品
 */
@RestController
@RequestMapping("/seckillGoods")
public class SeckillGoodsController
{
    @DubboReference
    private SeckillGoodsService seckillGoodsService;
    @DubboReference
    private SeckillService seckillService;
    
    /**
     * 新增秒杀商品
     * @param seckillGoods  秒杀商品实体
     * @return              返回结果
     */
    @PostMapping ("/add")
    public BaseResult add (@RequestBody SeckillGoods seckillGoods)
    {
        seckillGoodsService.add(seckillGoods);
        seckillService.removeFromBlacklist(seckillGoods.getGoodsId());
        
        return BaseResult.success();
    }
    
    /**
     * 修改秒杀商品
     * @param seckillGoods  秒杀商品实体
     * @return              修改结果
     */
    @PutMapping("/update")
    public BaseResult update (@RequestBody SeckillGoods seckillGoods)
    {
        seckillGoodsService.update(seckillGoods);
        seckillService.removeFromBlacklist(seckillGoods.getGoodsId());
        
        return BaseResult.success();
    }
    
    /**
     * 分页查询秒杀商品
     * @param page  页码
     * @param size  每页条数
     * @return      查询结果
     */
    @GetMapping ("/search")
    public BaseResult<Page<SeckillGoods>> findPage (int page, int size)
    {
        Page<SeckillGoods> page1 = seckillGoodsService.findPage(page, size);
        
        return BaseResult.success(page1);
    }
}
